
--[[
	***********************
	* MCServer - thomasfn *
	***********************
	defaulthooks.lua - Loads all default hooks
--]]

-- This should be up to date, so you can use as hook reference!

local def = {}

function def.PlayerConnect( ply )
	-- Called when the player authenticates
	util.BroadcastChat( ply:CNick() .. " has " .. COL_DARKGREEN .. "joined" .. COL_WHITE .. " the game." )
end

function def.PlayerSpawn( ply )
	-- Called when the player entity is created
	ply:SendChat( SERVER_MSG_COL .. "Welcome to " .. SERVER_TAG_COL .. SERVER_NAME .. SERVER_MSG_COL .. "!" )
end

function def.PlayerBanEnded( ply )
	-- Called when the player spawns after being banned
end

function def.PlayerDisconnect( ply, kicked, msg )
	-- Called when the player disconnects
	if (kicked) then
		util.BroadcastChat( ply:CNick() .. " was " .. COL_DARKRED .. "kicked" .. COL_WHITE .. " (" .. msg .. ")" )
	else
		if (msg == "Quitting") then msg = "By user" end
		util.BroadcastChat( ply:CNick() .. " has " .. COL_DARKGREEN .. "disconnected" .. COL_WHITE .. " (" .. msg .. ")" )
	end
end

function def.PlayerColour( ply, rank )
	-- Called when we need to know what colour the player's name is
	-- Admin mod plugins might want to override this
	if (rank == 1) then return COL_DARKRED end
	if (rank == 2) then return COL_GOLD end
	return COL_GRAY
end

function def.CNick( ply )
	-- ply:CNick() calls this
	return hooks.Call( "PlayerColour", { ply, ply:Rank() } ) .. ply:Nick() .. COL_WHITE
end

function def.PlayerChat( ply, msg )
	-- Called when the player chats
	if (string.len( msg ) > 0) then
		local fchar = string.sub( msg, 1, 1 )
		if (fchar == "/") or (fchar == "!") then
			local tmp = string.sub( msg, 2 )
			local args = table.FromString( tmp, " " )
			local cmd = string.lower( args[1] )
			table.remove( args, 1 )
			local handled = hooks.Call( "PlayerChatCommand", { ply, cmd, args } )
			if (handled) then return true end
		end
	end
	util.BroadcastChat( ply:CNick() .. ": " .. msg )
	print( ply:Nick() .. ": " .. msg )
end

local temp = {}
temp.__index = temp
function temp:Add( item )
	table.insert( self, item )
end

function def.PlayerChatCommand( ply, cmd, args )
	-- Called when the player runs a chat command (/command)
	-- If not handled, don't return anything
	if (cmd == "who") then
		local plist = core.EntityManager:GetAllPlayers()
		local tmp = {}
		for i=1, plist.Count do
			local pl = plist[i-1]
			tmp[i] = pl:CNick()
		end
		local str = "Players online: " .. table.concat( tmp, ", " ) .. " (" .. #tmp .. "/?)"
		ply:SendChat( str )
		return true
	end
	if (cmd == "spawn") then
		ply:Teleport( 8, 100, 8 )
		return true
	end
	if (cmd == "help") or (cmd == "commands") then
		local tmp = {}
		setmetatable( tmp, temp )
		hooks.Call( "ListChatCommands", { tmp } )
		ply:SendChat( "Commands are: " .. table.concat( tmp, ", " ) )
		return true
	end
	if (cmd == "pos") then
		local x = tostring( math.floor( ply:GetX() ) )
		local y = tostring( math.floor( ply:GetY() ) )
		local z = tostring( math.floor( ply:GetZ() ) )
		ply:SendChat( "You are at (" .. x .. ", " .. y .. ", " .. z .. ")" )
		return true
	end
	if (cmd == "temp") then
		local mgr = core.ChunkManager
		local temp = math.floor( mgr:GetMGTemp( mgr:BlockToChunkCoord( math.floor( ply:GetX() ) ), mgr:BlockToChunkCoord( math.floor( ply:GetZ() ) ) ) + 0.5 )
		ply:SendChat( "The temperature is currently about " .. temp .. " degrees celcius." )
		return true
	end
	if (cmd == "pvp") then
		local str = ""
		if (core.EntityManager:PVPEnabled()) then
			str = "PvP is currently " .. COL_DARKRED .. "on" .. COL_WHITE .. "."
		else
			str = "PvP is currently " .. COL_DARKGREEN .. "off" .. COL_WHITE .. "."
		end
		ply:SendChat( str )
		return true
	end
end

function def.ListChatCommands( tmp )
	tmp:Add( "/who" )
	tmp:Add( "/spawn" )
	tmp:Add( "/help" )
	tmp:Add( "/commands" )
	tmp:Add( "/pos" )
	tmp:Add( "/temp" )
	tmp:Add( "/pvp" )
end

function def.CanDigBlock( ply, bx, by, bz, oldid )
	-- Called when a player finishes digging a block
	return true
end

local BlockConvert = {
	[-1] = 0, -- Just in case
	[0] = 0, -- Just in case
	[1] = 4, -- Stone -> Cobblestone
	[2] = 3, -- Grass -> Dirt
	[78] = 332, -- Snow
	[8] = 0, -- Water
	[9] = 0, -- Water
	[10] = 0, -- Lava
	[11] = 0, -- Lava
	
}

function def.OnDigBlock( ply, bx, by, bz, oldid )
	-- Called when a player finishes digging a block, and is allowed to dig it
	local newid = BlockConvert[ oldid ] or oldid
	if (newid == 0) then return end
	core.EntityManager:SpawnItem( newid, bx, by, bz )
end

function def.CanPlaceBlock( ply, bx, by, bz, newid )
	-- Called when a player places a block
	return true
end

function def.OnPlaceBlock( ply, bx, by, bz, newid )
	-- Called just before a block update goes ahead, and the player is allowed to place it
	-- Return the block ID to place
	return newid
end

local DamageValues = {
	[268] = 3, -- Wood sword
	[272] = 4, -- Stone sword
	[267] = 5, -- Iron sword
	[276] = 6, -- Diamond sword
	
}

function def.PlayerUseEntity( ply, entid, leftclick )
	-- Called when the player clicks on an entity
	local held = ply:GetHeldItemID()
	local mgr = core.EntityManager
	local targetpl = mgr:GetPlayerByID( entid )
	if (ply) and (mgr:PVPEnabled()) then
		local dmg = DamageValues[ held ] or 1
		targetpl:TakeDamage( dmg )
	end
end

function def.PlayerUseBlock( ply, x, y, z, helditemid, blockid )
	-- Called then the player right clicks a block
	if (helditemid == 345) then
		ply:SendChat( "You are " .. math.ceil( math.sqrt( (x^2) + (z^2) ) ) .. " away from map origin." )
	end
end

hooks.Add( "default", def )